package cn.mccraft.uide.api.property.key;

import java.util.Objects;

import org.apache.commons.lang3.Validate;
import com.google.common.reflect.TypeToken;

import cn.mccraft.uide.api.property.value.Value;

public interface Key<V extends Value<?>> {
	
	TypeToken<V> getType();
	
	String getName();
	
	static <V extends Value<?>> Key<V> of(Class<V> type, String name) {
		Validate.notNull(type);
		Validate.notEmpty(name);
		return new Key<V>() {
			private final TypeToken<V> typeToken = TypeToken.of(type);

			@Override
			public TypeToken<V> getType() {
				return typeToken;
			}

			@Override
			public String getName() {
				return name;
			}
			
			@Override
			public boolean equals(Object obj) {
				if(!(obj instanceof Key<?>))
					return false;
				
				Key<?> key = (Key<?>)obj;
				
				return key.getType().equals(getType()) && key.getName().equals(getName());
			}
			
			@Override
			public int hashCode() {
				return Objects.hash(typeToken, name);
			}
			
			@Override
			public String toString() {
				return "{type:" + typeToken.toString() + ",name:" + name + "}";
			}
		};
	}
}
